package com.example.hello.client.Brand;

import com.example.hello.BootstrapBrand;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.communication.RpcProxy;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapBrand.class)
public class BootstrapBrandConnector extends AbstractComponentConnector implements ClickHandler {

    private BootstrapBrandRpc rpc = RpcProxy.create(BootstrapBrandRpc.class, this);

    public BootstrapBrandConnector() {
        getWidget().addClickHandler(this);
    }

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapBrand.class);
    }

    @Override
    public VBootstrapBrand getWidget() {
        return (VBootstrapBrand) super.getWidget();
    }

    @Override
    public BootstrapBrandState getState() {
        return (BootstrapBrandState) super.getState();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapBrandState state = getState();

        VBootstrapBrand brand = getWidget();

        brand.setEnabled(state.isBrandEnabled());
        brand.setHref(state.getHref());
        brand.setName(state.getName());
        brand.setTarget(state.getTarget());
        brand.setText(state.getText());
    }

    public void onClick(ClickEvent event) {
        MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(
                event.getNativeEvent(), getWidget().getElement());

        rpc.click(details);
    }

}